package br.com.knowledge.view.struts.actions;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.hibernate.Session;

import br.com.knowledge.persistence.hibernate.entities.BcdStoredprocedure;
import br.com.knowledge.persistence.hibernate.entities.BcdTabelas;
import br.com.knowledge.persistence.hibernate.entities.BcdTrigger;
import br.com.knowledge.persistence.hibernate.dao.BcdStoredprocedureDAO;
import br.com.knowledge.persistence.hibernate.dao.BcdTabelasDAO;
import br.com.knowledge.persistence.hibernate.dao.CadProjetoDAO;
import br.com.knowledge.persistence.jdbc.entities.StoredProcedure;
import br.com.knowledge.persistence.jdbc.entities.Trigger;
import br.com.knowledge.persistence.jdbc.services.JdbcServices;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/")
@ResultPath("/")
public class StoredProcedureAction extends ActionSupport implements SessionAware{
	
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
	}
	List<StoredProcedure> listaStoredProcedureSysDba = new ArrayList<StoredProcedure>();
	List<BcdStoredprocedure> listaStoredProcedure = new ArrayList<BcdStoredprocedure>();
	
	@SkipValidation
	@Action(value = "cadastra-storedprocedure", results = {
			@Result(name = SUCCESS, location = "/pages/listarStoredProcedures.jsp"),
			@Result(name = ERROR, location = "/pages/cadastraTipoDadoErro.jsp"),
			@Result(name = INPUT, location = "/pages/cadastraTipoDado.jsp") })	
	public String criarTabela(){		
		try {
			// primeiro vai buscar no banco de dados as tabelas
			listaStoredProcedureSysDba = new JdbcServices().getSysDbaStoredProcedure();
			BcdStoredprocedureDAO storedProcedureDAO = new BcdStoredprocedureDAO();
			Session session = storedProcedureDAO.getSession();
			session.getTransaction().begin();
			for(StoredProcedure spSysDba : listaStoredProcedureSysDba){
				BcdStoredprocedure storedProcedure = new BcdStoredprocedure();
				storedProcedure.setNomestoredprocedure(spSysDba.getName());
				try{
					storedProcedureDAO.save(storedProcedure);
				} catch(Exception e){
					session.getTransaction().rollback();
					return ERROR;
				}
			}
			session.getTransaction().commit();
			session.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} 			
		return SUCCESS;
	}
	
	@Action(value = "deletar-storedprocedure", results = {
			@Result(name = SUCCESS, location = "/pages/listarTiposDados.jsp"),
			@Result(name = ERROR, location = "/pages/cadastraTipoDadoErro.jsp"),
			@Result(name = INPUT, location = "/pages/cadastraTipoDado.jsp") })	
	public String deletarStoredProcedure(){
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	@Action(value = "listar-storedprocedure", results = {
			@Result(name = SUCCESS, location = "/pages/listarStoredProcedures.jsp"),
			@Result(name = ERROR, location = "/pages/cadastraTipoDadoErro.jsp"),
			@Result(name = INPUT, location = "/pages/cadastraTipoDado.jsp") })	
	public String listarStoredProcedure(){
		BcdStoredprocedureDAO storedProcedureDAO = new BcdStoredprocedureDAO();
		Session session = storedProcedureDAO.getSession();
		session.getTransaction().begin();
		try {
			//listaTableSysDba = new JdbcServices().getSysDbaTablesName();			
			listaStoredProcedure = storedProcedureDAO.findAll();
		} catch (Exception ex) {
			session.getTransaction().rollback();
		} finally {
			session.close();
		}		
		return SUCCESS;
	}
	
	@Action(value = "consultar-storedprocedure", results = {
			@Result(name = SUCCESS, location = "/pages/listarTiposDados.jsp"),
			@Result(name = ERROR, location = "/pages/cadastraTipoDadoErro.jsp"),
			@Result(name = INPUT, location = "/pages/cadastraTipoDado.jsp") })	
	public String consultarStoredProcedure(){
		return SUCCESS;
	}
}